package module.map
{
	import flash.filesystem.File;
	import flash.ui.Keyboard;
	
	import common.view.FGUIMediator;
	
	import fairygui.UIObjectFactory;
	import fairygui.UIPackage;
	
	import module.map.ImageResize.ImageResizeWindow;
	import module.map.ImageResize.ImageResizeWindowMediator;
	import module.map.fileTabs.FileTabMediator;
	import module.map.mapList.MapListMediator;
	import module.map.model.ProjectModel;
	import module.map.newMap.NewMapWindow;
	import module.map.newMap.NewMapWindowMediator;
	import module.map.nodeList.NodeListMediator;
	import module.map.nodes.AreaNodeMediator;
	import module.map.nodes.ImageNodeMediator;
	import module.map.nodes.PathNodeMediator;
	import module.map.nodes.PointMeidator;
	import module.map.nodes.TowerNodeMediator;
	import module.map.nodes.newNode.NewNodeWindow;
	import module.map.nodes.newNode.NewNodeWindowMediator;
	import module.map.operations.base.CopyOperation;
	import module.map.operations.base.DeleteOperation;
	import module.map.operations.base.PasteOperation;
	import module.map.operations.base.RedoOperation;
	import module.map.operations.base.SaveOperation;
	import module.map.operations.base.UndoOperation;
	import module.map.operations.nodes.DeleteNodeOperation;
	import module.map.operations.nodes.NewNodeOperation;
	import module.map.operations.points.AddPointOperation;
	import module.map.operations.points.RemovePointOperation;
	import module.map.operations.propertys.ArrayPropertyChangedOperation;
	import module.map.operations.propertys.CollectionPropertyChangedOperation;
	import module.map.propertys.NumberPropertyViewMediator;
	import module.map.propertys.PathPropertyViewMediator;
	import module.map.propertys.PropertyPanelMediator;
	import module.map.propertys.StringPropertyViewMediator;
	import module.map.scenes.SceneMediator;
	import module.map.topButtons.TopButtonMediator;
	
	import robotlegs.bender.extensions.eventCommandMap.api.IEventCommandMap;
	import robotlegs.bender.extensions.mediatorMap.api.IMediatorMap;
	import robotlegs.bender.framework.api.IContext;
	import robotlegs.bender.framework.api.IInjector;
	
	import ui.Basic.UI_FileTabButton;
	import ui.Map.UI_AreaNodeView;
	import ui.Map.UI_FileTabPanel;
	import ui.Map.UI_ImageNodeView;
	import ui.Map.UI_MapListPanel;
	import ui.Map.UI_MapWindow;
	import ui.Map.UI_NodePanel;
	import ui.Map.UI_NumberPropertyView;
	import ui.Map.UI_PathNodeView;
	import ui.Map.UI_PathPropertyView;
	import ui.Map.UI_PointView;
	import ui.Map.UI_PropertyPanel;
	import ui.Map.UI_ScenePanel;
	import ui.Map.UI_StringPropertyView;
	import ui.Map.UI_TopButtonPanel;
	import ui.Map.UI_TowerNodeView;
	import module.map.fileTabs.FileTabButton;
	
	public class MapMediator extends FGUIMediator
	{
		[Inject]
		public var inject:IInjector;
		[Inject]
		public var context:IContext;
		[Inject]
		public var mediatorMap:IMediatorMap;
		[Inject]
		public var commandMap:IEventCommandMap;
		[Inject]
		public var view:UI_MapWindow;
		[Inject]
		public var projectModel:ProjectModel;
		
		public function MapMediator()
		{
			super();
		}
		
		override public function initialize():void{
			super.initialize();
			var dicFile:File=this.view.data as File;
			if(dicFile==null){
				throw new Error("项目不存在！");
			}
			this.projectModel.Init(dicFile,this.view.root,this.view.displayObject.stage,context);
			this.projectModel.RefreshMapList();
			
			this.InitItemExtension();
			this.CreateSubMediator();
			this.RegisterOperations();
			this.RegisterShotKeys();
		}
		
		private function RegisterOperations():void{
			this.projectModel.undoSystem.registerOperation(Operations.UNDO,UndoOperation);
			this.projectModel.undoSystem.registerOperation(Operations.REDO,RedoOperation);
			this.projectModel.undoSystem.registerOperation(Operations.COPY,CopyOperation);
			this.projectModel.undoSystem.registerOperation(Operations.PASTE,PasteOperation);
			this.projectModel.undoSystem.registerOperation(Operations.DELETE,DeleteOperation);
			this.projectModel.undoSystem.registerOperation(Operations.SAVE,SaveOperation);
			
			this.projectModel.undoSystem.registerOperation(Operations.COLLECTION_PROPERTYS_CHANGE,CollectionPropertyChangedOperation);
			this.projectModel.undoSystem.registerOperation(Operations.ARRAY_PROPERTYS_CHANGE,ArrayPropertyChangedOperation);
			
			//节点
			this.projectModel.undoSystem.registerOperation(Operations.NEW_NODE,NewNodeOperation);
			this.projectModel.undoSystem.registerOperation(Operations.DELETE_NODE,DeleteNodeOperation);
			
			//点
			this.projectModel.undoSystem.registerOperation(Operations.ADD_POINT,AddPointOperation);
			this.projectModel.undoSystem.registerOperation(Operations.REMOVE_POINT,RemovePointOperation);
			
		}
		
		private function RegisterShotKeys():void{
			this.projectModel.undoSystem.registerShortKey(Operations.UNDO,[Keyboard.CONTROL,Keyboard.Z]);
			this.projectModel.undoSystem.registerShortKey(Operations.REDO,[Keyboard.CONTROL,Keyboard.SHIFT,Keyboard.Z]);
			this.projectModel.undoSystem.registerShortKey(Operations.DELETE,[Keyboard.DELETE]);
			this.projectModel.undoSystem.registerShortKey(Operations.COPY,[Keyboard.CONTROL,Keyboard.C]);
			this.projectModel.undoSystem.registerShortKey(Operations.PASTE,[Keyboard.CONTROL,Keyboard.V]);
			this.projectModel.undoSystem.registerShortKey(Operations.SAVE,[Keyboard.CONTROL,Keyboard.S]);
		}
		
		private function CreateSubMediator():void{
			
			//新建地图
			mediatorMap.map(NewMapWindow).toMediator(NewMapWindowMediator);
			//图片等比缩放
			mediatorMap.map(ImageResizeWindow).toMediator(ImageResizeWindowMediator);
			
			mediatorMap.map(UI_MapListPanel).toMediator(MapListMediator);
			mediatorMap.map(UI_ScenePanel).toMediator(SceneMediator);
			mediatorMap.map(UI_TopButtonPanel).toMediator(TopButtonMediator);
			mediatorMap.map(UI_FileTabPanel).toMediator(FileTabMediator);
			mediatorMap.map(UI_NodePanel).toMediator(NodeListMediator);
			mediatorMap.map(UI_PropertyPanel).toMediator(PropertyPanelMediator);
			mediatorMap.map(NewNodeWindow).toMediator(NewNodeWindowMediator);
			
			//节点
			mediatorMap.map(UI_PathNodeView).toMediator(PathNodeMediator);
			mediatorMap.map(UI_ImageNodeView).toMediator(ImageNodeMediator);
			mediatorMap.map(UI_AreaNodeView).toMediator(AreaNodeMediator);
			mediatorMap.map(UI_TowerNodeView).toMediator(TowerNodeMediator);
			//点
			mediatorMap.map(UI_PointView).toMediator(PointMeidator);
			
			//实例化
			mediatorMap.mediate(this.view.m_mapList);
			mediatorMap.mediate(this.view.m_scenePanel);
			mediatorMap.mediate(this.view.m_TopButtonPanel);
			mediatorMap.mediate(this.view.m_fileTabPanel);
			mediatorMap.mediate(this.view.m_nodePanel);
			mediatorMap.mediate(this.view.m_propertyPanel);
			
			//属性
			mediatorMap.map(UI_StringPropertyView).toMediator(StringPropertyViewMediator);
			mediatorMap.map(UI_NumberPropertyView).toMediator(NumberPropertyViewMediator);
			mediatorMap.map(UI_PathPropertyView).toMediator(PathPropertyViewMediator);
		}
		
		private function InitItemExtension():void{
			UIObjectFactory.setPackageItemExtension(UIPackage.getItemURL("Basic","FileTabButton"), FileTabButton);
		}
	}
}